package fpt.edu.vn.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import fpt.edu.vn.dao.AddnewDao;
import fpt.edu.vn.dao.TopicsDao;
import fpt.edu.vn.dao.UsersDao;
import fpt.edu.vn.model.Topicimages;
import fpt.edu.vn.model.Topics;

/* 
 * DetailAction.java 
 *  
 * 0.1
 * 
 * 10/15/2012 
 *  
 * Copyright notice 
 *  
 * Modification Logs: 
 *   DATE        AUTHOR      DESCRIPTION 
 *  -------------------------------------------------------- 
 *  15-Oct-2012  ThuongND    Create new 
 */
public class DeleteImgAction extends ActionSupport implements ServletRequestAware{

	private static final long serialVersionUID = 1L;
	private int topicId;
	private TopicsDao dao = new TopicsDao();
	private List<Topics> ownerFavorite;
	private int userId;
	private int isOwner;
	private int isAdmin;
	private List<Object> checkOwnerTopic;
	private List<Object> checkAdmin;
	private List<Topicimages> lstimages;
	private int topicImageId;
	private HttpServletRequest servletRequest;
	private AddnewDao addnewdao = new AddnewDao();
	// For SessionAware
	Map<String, Object> session;

	

	public String execute() {
		String email = "";
		session = ActionContext.getContext().getSession();
		if (session.containsKey("emailss")) {
			email = (String) session.get("emailss");
			userId = (Integer) session.get("userIdss");
			checkOwnerTopic = dao.checkOwnerTopic(email, topicId);
			checkAdmin = dao.checkAdmin(email);
			if (checkAdmin.size() < 1) {
			} else {
				setIsAdmin(1);
			}
			if (checkOwnerTopic.size() < 1) {
			} else {
				setIsOwner(1);
			}
			if (isAdmin == 1 || isOwner == 1) {
				//delete img
				String temp = dao.topicimg(topicImageId).get(0).getPath();
				dao.deletedImgTopic(topicImageId);
				// list img again
				this.lstimages = dao.image(topicId);
				if(lstimages.size()==0){
					addnewdao.updateImg(topicId);
				}
				try{
					String filePath = servletRequest.getRealPath("/Img/Topic/");
					
					File file = new File(filePath+"/"+temp);
					if(file.delete()){
		    			System.out.println(file.getName() + " is deleted!");
		    		}else{
		    			System.out.println("Delete operation is failed.");
		    		}
				}catch (Exception e) {
					e.printStackTrace();
				}
			}
			
		}
		
		return SUCCESS;

	}
	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;

	}
	public int getTopicId() {
		return topicId;
	}
	public void setTopicId(int topicId) {
		this.topicId = topicId;
	}
	public List<Topics> getOwnerFavorite() {
		return ownerFavorite;
	}

	public void setOwnerFavorite(List<Topics> ownerFavorite) {
		this.ownerFavorite = ownerFavorite;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}
	public List<Topicimages> getLstimages() {
		return lstimages;
	}
	public void setLstimages(List<Topicimages> lstimages) {
		this.lstimages = lstimages;
	}
	public int getIsOwner() {
		return isOwner;
	}
	public void setIsOwner(int isOwner) {
		this.isOwner = isOwner;
	}
	public int getIsAdmin() {
		return isAdmin;
	}
	public void setIsAdmin(int isAdmin) {
		this.isAdmin = isAdmin;
	}
	public List<Object> getCheckOwnerTopic() {
		return checkOwnerTopic;
	}
	public void setCheckOwnerTopic(List<Object> checkOwnerTopic) {
		this.checkOwnerTopic = checkOwnerTopic;
	}
	public List<Object> getCheckAdmin() {
		return checkAdmin;
	}
	public void setCheckAdmin(List<Object> checkAdmin) {
		this.checkAdmin = checkAdmin;
	}
	public int getTopicImageId() {
		return topicImageId;
	}
	public void setTopicImageId(int topicImageId) {
		this.topicImageId = topicImageId;
	}
	
	
}
